from django.shortcuts import render

from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView

from apps.publish.models import PublishModel
from apps.index.serializers import IndexSerializers

from utils.pagination import DataLimitOffsetPagination
from utils.filter import MinIdBaseFilterBackend,MaxIdBaseFilterBackend

# # 首页展示
# class IndexView(APIView):
#     def get(self,request):
#         min_id = request.query_params.get("min_id")
#         max_id = request.query_params.get("max_id")
#         print(min_id)
#         print(max_id)
#         if min_id:
#             queryset = PublishModel.objects.filter(id__lt=min_id).order_by("-id")[0:10]
#         elif max_id:
#             queryset = PublishModel.objects.filter(id__gt=max_id).order_by("id")[0:10]
#         else:
#             # 从数据库中去除所有数据，并倒序，然后列出前十条数据
#             queryset = PublishModel.objects.all().order_by("-id")[0:10]
#         ser = IndexSerializers(instance=queryset,many=True)
#         return Response(ser.data,status=200)


class IndexView(ListAPIView):
    serializer_class = IndexSerializers
    queryset = PublishModel.objects.all().order_by('-id')
    pagination_class = DataLimitOffsetPagination #自定义分页器
    filter_backends = [MinIdBaseFilterBackend,MaxIdBaseFilterBackend] #自定义切片